package com.young.weixin.model.message;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.young.weixin.model.enums.EWxEventTypeEnum;
import com.young.weixin.model.enums.EWxMessageTypeEnum;

/**
 * 事件消息
 *
 * @author wangjiyu
 * @date 2021/10/5 17:05
 */
@XStreamAlias("xml")
public class WxEventMessage extends WxBaseMessage {
    /**
     * 事件类型
     * {@link com.young.weixin.model.enums.EWxEventTypeEnum}
     */
    @XStreamAlias("Event")
    private String event;
    /**
     * event = subscribe(且是扫码后关注的), 事件KEY值，qrscene_为前缀，后面为二维码的参数值
     * event = CLICK, 事件KEY值，与自定义菜单接口中KEY值对应
     * event = VIEW, 事件KEY值，设置的跳转URL
     */
    @XStreamAlias("EventKey")
    private String eventKey;
    /**
     * 二维码的ticket，可用来换取二维码图片
     */
    @XStreamAlias("Ticket")
    private String ticket;

    // 上报地理位置事件专属字段 start
    /**
     * 地理位置纬度
     */
    @XStreamAlias("Latitude")
    private String latitude;
    /**
     * 地理位置经度
     */
    @XStreamAlias("Longitude")
    private String longitude;
    /**
     * 地理位置精度
     */
    @XStreamAlias("Precision")
    private String precision;
    // 上报地理位置事件专属字段 end


    /**
     * 判断事件类型
     *
     * @param typeEnum
     * @return true表示匹配成功
     */
    public boolean eventIs(EWxEventTypeEnum typeEnum) {
        return typeEnum.eq(this.event);
    }

    /**
     * 获取事件类型枚举
     *
     * @return
     */
    public EWxEventTypeEnum event() {
        return EWxEventTypeEnum.of(this.event);
    }

    /**
     * 构造消息对象
     *
     * @param fromUser 发送人
     * @param toUser   接收人
     * @return 消息对象
     */
    @Override
    public WxEventMessage build(String fromUser, String toUser) {
        WxEventMessage message = new WxEventMessage();
        message.fillWxBaseMessage(fromUser, toUser, EWxMessageTypeEnum.EVENT);
        return message;
    }

    /**
     * 构造消息对象
     *
     * @param messageMapWrapper 参数map
     * @return 消息对象
     */
    @Override
    public WxEventMessage convert(WxMessageMapWrapper messageMapWrapper) {
        WxEventMessage message = new WxEventMessage();
        message.fillWxBaseMessage(messageMapWrapper);
        message.setEvent(messageMapWrapper.getEvent());
        message.setEventKey(messageMapWrapper.getEventKey());
        message.setTicket(messageMapWrapper.getTicket());
        message.setLatitude(messageMapWrapper.getLatitude());
        message.setLongitude(messageMapWrapper.getLongitude());
        message.setPrecision(messageMapWrapper.getPrecision());
        return message;
    }

    public WxEventMessage() {
        super();
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getEventKey() {
        return eventKey;
    }

    public void setEventKey(String eventKey) {
        this.eventKey = eventKey;
    }

    public String getTicket() {
        return ticket;
    }

    public void setTicket(String ticket) {
        this.ticket = ticket;
    }

    public String getLatitude() {
        return latitude;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public String getPrecision() {
        return precision;
    }

    public void setPrecision(String precision) {
        this.precision = precision;
    }
}
